from django.contrib.auth.models import AbstractUser
from django.db import models


# 首页通知
class Notice(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, verbose_name="标题")
    content = models.TextField(verbose_name="简要内容")
    contentDetail = models.TextField(verbose_name="详细内容")
    createdAt = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = 'notices'
        verbose_name = '通知公告'
        verbose_name_plural = '通知公告'





class BgPic(models.Model):
    id = models.AutoField(primary_key=True)
    imgs = models.CharField(max_length=255, verbose_name="图片路径")

    class Meta:
        db_table = 'bgpic'
        verbose_name = '背景图片'
        verbose_name_plural = '背景图片'

    def __str__(self):
        return f"背景图片 {self.id}"





class Enotice(models.Model):
    id = models.AutoField(primary_key=True)
    time = models.DateTimeField(verbose_name="时间")
    content = models.TextField(verbose_name="内容")

    class Meta:
        db_table = 'enotices'
        verbose_name = '紧急通知'
        verbose_name_plural = '紧急通知'

    def __str__(self):
        return f"紧急通知 {self.id}"


class LostFound(models.Model):
    CATEGORY_CHOICES = (
        ('lost', '失物'),
        ('found', '招领'),
    )
    id = models.AutoField(primary_key=True)
    description = models.TextField(null=True, blank=True, verbose_name="描述")
    imgs = models.CharField(max_length=255, null=True, blank=True, verbose_name="图片")
    tel = models.CharField(max_length=20, null=True, blank=True, verbose_name="联系电话")
    name = models.CharField(max_length=255, null=True, blank=True, verbose_name="联系人")
    category = models.CharField(max_length=10, choices=CATEGORY_CHOICES, verbose_name="类别")
    time = models.DateTimeField(null=True, blank=True, verbose_name="时间")
    location = models.CharField(max_length=255, null=True, blank=True, verbose_name="地点")

    class Meta:
        db_table = 'lostfound'
        verbose_name = '失物招领'
        verbose_name_plural = '失物招领'

    def __str__(self):
        return f"{self.get_category_display()} - {self.id}"


class User(AbstractUser):
    GENDER_CHOICES = (
        ('M', '男'),
        ('F', '女'),
        ('U', '未知'),
    )
    
    avatar = models.CharField(max_length=255, null=True, blank=True, verbose_name="头像")
    tel = models.CharField(max_length=20, null=True, blank=True, verbose_name="电话")
    location = models.CharField(max_length=255, null=True, blank=True, verbose_name="位置")
    age = models.IntegerField(null=True, blank=True, verbose_name="年龄")
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='U', verbose_name="性别")

    class Meta:
        db_table = 'users'
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.username

class Active(models.Model):
    CATEGORY_CHOICES = (
        ('文化教育', '文化教育'),
        ('体育健康', '体育健康'),
        ('志愿服务', '志愿服务'),
        ('节日主题', '节日主题'),
    )
    id = models.AutoField(primary_key=True)
    cover = models.CharField(max_length=255, null=True, blank=True, verbose_name="封面图片")
    category = models.CharField(max_length=255, choices=CATEGORY_CHOICES, verbose_name="活动分类")
    title = models.CharField(max_length=255, verbose_name="标题")
    ac_description = models.TextField(null=True, blank=True, verbose_name="活动描述")
    userId = models.ForeignKey(null=True,to=User, blank=True, verbose_name="发布人",on_delete=models.SET_NULL)
    browseCount = models.IntegerField(default=0, verbose_name="浏览次数")
    location = models.CharField(max_length=255, verbose_name="地点")
    description = models.CharField(max_length=255, null=True, blank=True, verbose_name="简要描述")
    participater = models.IntegerField(default=0, verbose_name="参与人数")

    class Meta:
        db_table = 'actives'
        verbose_name = '活动'
        verbose_name_plural = '活动'

    def __str__(self):
        return self.title

class Property(models.Model):
    TYPE_CHOICES = (
        ('学区房', '学区房'),
        ('商品房', '商品房'),
        ('安置房', '安置房'),
        ('无', '无'),
    )
    id = models.AutoField(primary_key=True)
    floor = models.CharField(max_length=255, null=True, blank=True, verbose_name="楼层")
    userId = models.ForeignKey(null=True, to=User, blank=True, verbose_name="用户ID", on_delete=models.SET_NULL)
    type = models.CharField(max_length=10, choices=TYPE_CHOICES, verbose_name="房产类型")
    address = models.CharField(max_length=255, null=True, blank=True, verbose_name="地址")
    area = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name="面积")
    layout = models.CharField(max_length=255, null=True, blank=True, verbose_name="户型")

    class Meta:
        db_table = 'property'
        verbose_name = '房产'
        verbose_name_plural = '房产'

    def __str__(self):
        return f"房产 {self.id} - 用户 {self.userId}"


class Repair(models.Model):
    STATUS_CHOICES = (
        ('processed', '已处理'),
        ('processing', '处理中'),
        ('unprocessed', '未处理'),
    )
    
    TYPE_CHOICES = (
        ('水电维修', '水电维修'),
        ('电梯故障', '电梯故障'),
        ('门窗维修', '门窗维修'),
    )
    id = models.AutoField(primary_key=True)

    userId = models.ForeignKey(User, null=True, blank=True, verbose_name="报修人员", on_delete=models.SET_NULL)
    time = models.DateTimeField(verbose_name="报修时间")
    type = models.CharField(max_length=20, choices=TYPE_CHOICES, verbose_name="维修类型")
    problem = models.TextField(verbose_name="问题描述")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name="处理状态")
    handleResult = models.TextField(null=True, blank=True, verbose_name="处理结果")

    class Meta:
        db_table = 'repair'
        verbose_name = '报修'
        verbose_name_plural = '报修'

    def __str__(self):
        return f"报修 {self.id} - 用户 {self.userId}"


class Staff(models.Model):
    objects = None
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="姓名")
    position = models.CharField(max_length=255, null=True, blank=True, verbose_name="职位")
    tel = models.CharField(max_length=20, null=True, blank=True, verbose_name="电话")

    class Meta:
        db_table = 'staffs'
        verbose_name = '物业人员'
        verbose_name_plural = '物业人员'

    def __str__(self):
        return f"{self.name} - {self.position or '未知职位'}"

class Bill(models.Model):
    CATEGORY_CHOICES = (
        ('property', '物业费'),
        ('parking', '停车费'),
        ('electric', '电费'),
        ('water', '水费'),
    )
    id = models.AutoField(primary_key=True)
    userId = models.ForeignKey(null=True, to=User, blank=True, verbose_name="用户ID", on_delete=models.SET_NULL)
    catagory = models.CharField(max_length=20, choices=CATEGORY_CHOICES, verbose_name="账单类别")
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="金额")
    paid = models.BooleanField(verbose_name="是否已支付")
    description = models.TextField(null=True, blank=True, verbose_name="描述")
    billtime = models.DateTimeField(null=True, blank=True, verbose_name="账单时间")

    class Meta:
        db_table = 'bill'
        verbose_name = '账单'
        verbose_name_plural = '账单'

    def __str__(self):
        return f"{self.get_catagory_display()} - {self.amount}"


class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.ForeignKey(null=True, to=User, blank=True, verbose_name="评论人员", on_delete=models.SET_NULL)
    content = models.TextField(verbose_name="评论内容")
    activesId = models.ForeignKey(null=True, to=Active, blank=True, verbose_name="活动信息", on_delete=models.SET_NULL)

    class Meta:
        db_table = 'comments'
        verbose_name = '评论'
        verbose_name_plural = '评论'

    def __str__(self):
        return f"评论 {self.id} - 用户 {self.userId}"

class SearchRecord(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.ForeignKey(null=True, to=User, blank=True, verbose_name="搜索人员", on_delete=models.SET_NULL)
    searchTime = models.DateTimeField(auto_now_add=True, verbose_name="搜索时间")
    searchKeyword = models.CharField(max_length=255, null=True, blank=True, verbose_name="搜索关键词")

    class Meta:
        db_table = 'search_records'
        verbose_name = '活动搜索记录'
        verbose_name_plural = '活动搜索记录'
        ordering = ['-searchTime']  # 按搜索时间倒序排序

    def __str__(self):
        return f"用户{self.userId}的搜索记录 - {self.searchTime}"


class ActivityRegistration(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="报名用户")
    activityId = models.ForeignKey(Active, on_delete=models.CASCADE, verbose_name="活动")
    registrationTime = models.DateTimeField(auto_now_add=True, verbose_name="报名时间")
    remark = models.TextField(null=True, blank=True, verbose_name="备注")

    class Meta:
        db_table = 'activity_registrations'
        verbose_name = '活动报名'
        verbose_name_plural = '活动报名'
        unique_together = ('userId', 'activityId')  # 防止重复报名

    def __str__(self):
        return f"{self.userId.username} - {self.activityId.title}"

class PropertyUserRelation(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    staffId = models.ForeignKey(Staff, on_delete=models.CASCADE, verbose_name="物业人员")


    class Meta:
        db_table = 'property_user_relations'
        verbose_name = '物业用户关系'
        verbose_name_plural = '物业用户关系'


    def __str__(self):
        return f"{self.userId.username} - {self.staffId.name}"
